package com.heima.wemedia.interceptor;

import com.heima.model.wemedia.pojos.WmUser;
import com.heima.utils.common.WmThreadLocalUtil;
import com.heima.wemedia.service.WmUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Optional;
@Slf4j
public class WmTokenInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String userId = request.getHeader("userId");
        log.info("userId:{}",userId);
        Optional<String> optional= Optional.ofNullable(userId);
       if(optional.isPresent()){//如果存在，说明传进去的不是空值
           WmUser wmUser=new WmUser();
           wmUser.setId(Integer.valueOf(userId));
           WmThreadLocalUtil.setUser(wmUser);
           log.info("拦截器拦截成功ggggggg,wmTokenFilter设置用户信息到线程中...");
       }
       return true;
    }
    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
     log.info("拦截器清理用户信息");
     WmThreadLocalUtil.clear();
    }
}
